package com.datounet.flutterapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.just.agentweb.AgentWeb;
import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;

public class AgentWebActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG = AgentWebActivity.class.getSimpleName();
    public static final String INIT_URL_PARAM ="URL";

    LinearLayout linearLayout;
    ImageButton btnBack, btnClose;
    TextView txtTitle;
    AgentWeb mAgentWeb;
    String url;

    public static void openWebView(Context context, String url) {
        Intent intent = new Intent(context, AgentWebActivity.class);
        intent.putExtra(INIT_URL_PARAM, url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agent_web);

        linearLayout = findViewById(R.id.web_container);
        btnBack = findViewById(R.id.act_web_btn_back);
        btnBack.setOnClickListener(this);
        txtTitle = findViewById(R.id.act_web_txt_title);
        btnClose = findViewById(R.id.act_web_btn_close);
        btnClose.setOnClickListener(this);

        url = getIntent().getStringExtra(INIT_URL_PARAM);
        Log.e(TAG, "url:" + url);

        WebViewClient mWebViewClient = new WebViewClient() {

        };
        WebChromeClient mWebChromeClient = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                if (title != null) {
                    txtTitle.setText(title);
                }
                super.onReceivedTitle(view, title);
            }
        };
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(linearLayout, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go(url);
        mAgentWeb.getJsInterfaceHolder().addJavaObject("AndroidWebView", new AndroidInterface(mAgentWeb, this));
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (!mAgentWeb.back()) {
            this.finish();
        }else {
            return;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.act_web_btn_back:
                if (!mAgentWeb.back()) {
                    this.finish();
                }
                break;
            case R.id.act_web_btn_close:
                this.finish();
                break;
            default:
                break;
        }
    }



    private static class AndroidInterface {

        private AgentWeb agent;
        private Activity context;

        public AndroidInterface(AgentWeb agent, Activity context) {
            this.agent = agent;
            this.context = context;
        }


        @JavascriptInterface
        public void hello(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            agent.getJsAccessEntrace().quickCallJs("callJS", "Hello ! Agentweb");
        }

    }
}